/**
 * 
 */
package com.bingya.service.system.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.flex.remoting.RemotingDestination;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bingya.dao.UserMapper;
import com.bingya.dao.UserRoleMapper;
import com.bingya.model.Page;
import com.bingya.model.User;
import com.bingya.model.UserExample;
import com.bingya.model.UserRole;
import com.bingya.model.UserRoleExample;
import com.bingya.service.system.IUserService;

/**
 * @author Administrator
 * 
 */
@Transactional
@Service(value = "userService")
@RemotingDestination(value = "userService", channels = { "my-amf",
		"my-secure-amf" })
@Secured(value = "ROLE_ADMIN")
public class UserServiceImpl implements IUserService {

	@Autowired(required = true)
	private UserMapper userMapper;

	@Autowired(required = true)
	private UserRoleMapper userRoleMapper;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bingya.service.system.IGenericService#deleteByPrimaryKey(java.lang
	 * .Integer)
	 */
	@Override
	public int deleteByPrimaryKey(Integer id) {
		// 删除子表数据
		UserRoleExample userRoleExam = new UserRoleExample();
		userRoleExam.createCriteria().andUserIdEqualTo(id);
		List<UserRole> list = userRoleMapper.selectByExample(userRoleExam);
		for (UserRole userRole : list) {
			userRoleMapper.deleteByPrimaryKey(userRole.getId());
		}

		return userMapper.deleteByPrimaryKey(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bingya.service.system.IGenericService#insert(java.io.Serializable)
	 */
	@Override
	public int insert(User entity) {
		// ---注意flex前台传过来的值为0
		if (entity.getId() == 0) {
			entity.setId(null);
		}

		int i = userMapper.insertSelective(entity);
		return i;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.bingya.service.system.IGenericService#selectAll()
	 */
	@Override
	public List<User> selectAll() {
		List<User> list = userMapper.selectByExample(null);
		return list;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bingya.service.system.IGenericService#selectByPrimaryKey(java.lang
	 * .Integer)
	 */
	@Override
	public User selectByPrimaryKey(Integer id) {
		User user = userMapper.selectByPrimaryKey(id);
		return user;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bingya.service.system.IGenericService#update(java.io.Serializable)
	 */
	@Override
	public int update(User entity) {
		// ---注意flex前台传过来的值为0
		if (entity.getId() == 0) {
			entity.setId(null);
		}
		
		int i = userMapper.updateByExampleSelective(entity, null);
		return i;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.bingya.service.system.IGenericService#findPage(com.bingya.model.Page)
	 */
	@Override
	public Page findPage(Page page) {
		UserExample userExample = new UserExample();
		page.setCount(userMapper.countByExample(userExample));

		userExample.setStart(page.getCurrent() * page.getLength());
		userExample.setLimit(page.getLength());
		List<User> list = userMapper.selectByExample(userExample);
		page.setList(list);

		return page;
	}

	@Override
	public void login(String username, String password) {
		
	}

}
